//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmBlackScholesSolver.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSolverDesc.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double strike, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    CFdmSolverDesc^ _CsolverDesc;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdmBlackScholesSolver = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        Handle<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<Handle<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetHandle ()); 
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        _CsolverDesc = safe_cast<CFdmSolverDesc^> (solverDesc);
        _CsolverDesc->Lock();
        QuantLib::FdmSolverDesc& _solverDesc = static_cast<QuantLib::FdmSolverDesc&> (_CsolverDesc->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Douglas()); //1
        bool _localVol = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (localVol) ? (bool)ValueHelper::Convert (localVol->Value) : false); //4
        QuantLib::Real _illegalLocalVolOverwrite = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (illegalLocalVolOverwrite) ? (QuantLib::Real)ValueHelper::Convert (illegalLocalVolOverwrite->Value) : -Null<QuantLib::Real>()); //4
        _ppFdmBlackScholesSolver = new boost::shared_ptr<QuantLib::FdmBlackScholesSolver> (new QuantLib::FdmBlackScholesSolver ( _process,  _strike,  _solverDesc,  _schemeDesc,  _localVol,  _illegalLocalVolOverwrite ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_CsolverDesc != nullptr) _CsolverDesc->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (boost::shared_ptr<QuantLib::FdmBlackScholesSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	_ppFdmBlackScholesSolver = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::FdmBlackScholesSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	_ppFdmBlackScholesSolver = new boost::shared_ptr<QuantLib::FdmBlackScholesSolver> (&childNative);
    
    _FdmBlackScholesSolverOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (CFdmBlackScholesSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmBlackScholesSolver = new boost::shared_ptr<QuantLib::FdmBlackScholesSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmBlackScholesSolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::Handle<QuantLib::FdmBlackScholesSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBlackScholesSolver = &childNative;
	_ppFdmBlackScholesSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesSolver>>(childNative.currentLink());
    
    _FdmBlackScholesSolverOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::Handle<QuantLib::FdmBlackScholesSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmBlackScholesSolver = &childNative;
	_ppFdmBlackScholesSolver = &static_cast<boost::shared_ptr<QuantLib::FdmBlackScholesSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::CFdmBlackScholesSolver (QuantLib::FdmBlackScholesSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmBlackScholesSolver = NULL;
#endif
	_ppFdmBlackScholesSolver = new boost::shared_ptr<QuantLib::FdmBlackScholesSolver> (new QuantLib::FdmBlackScholesSolver (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::~CFdmBlackScholesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmBlackScholesSolver != NULL)
    {
	    delete _ppFdmBlackScholesSolver;
        _ppFdmBlackScholesSolver = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::!CFdmBlackScholesSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmBlackScholesSolver != NULL)
    {
	    delete _ppFdmBlackScholesSolver;
    }
}
QuantLib::FdmBlackScholesSolver& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::GetReference ()
{
    if (_ppFdmBlackScholesSolver == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmBlackScholesSolver;
}
boost::shared_ptr<QuantLib::FdmBlackScholesSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::GetShared ()
{
    if (_ppFdmBlackScholesSolver == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmBlackScholesSolver;
}
QuantLib::FdmBlackScholesSolver* Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::GetPointer ()
{
    if (_ppFdmBlackScholesSolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmBlackScholesSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmBlackScholesSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::GetHandle ()
{
	if (_phFdmBlackScholesSolver == NULL)
	{
		_phFdmBlackScholesSolver = new Handle<QuantLib::FdmBlackScholesSolver> (*_ppFdmBlackScholesSolver);
	}
	return *_phFdmBlackScholesSolver;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::HasNative () 
{
	return (_ppFdmBlackScholesSolver != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::DeltaAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->deltaAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::GammaAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->gammaAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::ThetaAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->thetaAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver::ValueAt (Double s)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmBlackScholesSolver)->valueAt ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Solvers::IFdmBlackScholesSolver^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmBlackScholesSolver_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double strike, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc, Microsoft::FSharp::Core::FSharpOption<Boolean>^ localVol, Microsoft::FSharp::Core::FSharpOption<Double>^ illegalLocalVolOverwrite)
{
    return REFNEW CFdmBlackScholesSolver ( process,  strike,  solverDesc,  schemeDesc,  localVol,  illegalLocalVolOverwrite);
}
